#include "settingsmgr.h"
#include "QSize"

#include "QDebug"

SettingsMgr::SettingsMgr()
{
    ini = new QSettings("set.ini", QSettings::IniFormat);
    ini->setIniCodec("UTF-8"); //关键

//    writeTest();
//    writeTest2();
}

SettingsMgr::~SettingsMgr()
{
    delete ini;
}

void SettingsMgr::writeTest()
{
    QString sSection = "peopleList";

    ini->beginWriteArray(sSection);
    for (int i = 0; i < 10; ++i) {
        ini->setArrayIndex(i);
        ini->setValue("userName", QString("NN%1").arg(i));
        ini->setValue("password", QString("PW%1").arg(i*i));
    }
    ini->endArray();


    ini->beginGroup("mainwindow");
    ini->setValue("size", QSize(12,12));
    ini->setValue("fullScreen", false);

    {
        ini->beginGroup("sub");

        ini->setValue("size", QSize(12,12));
        ini->setValue("fullScreen", false);

        //array
        {
            QString sSection = "peopleList";

            ini->beginWriteArray(sSection);
            for (int i = 0; i < 10; ++i) {
                ini->setArrayIndex(i);
                ini->setValue("userName", QString("NN%1").arg(i));
                ini->setValue("password", QString("PW%1").arg(i*i));
            }
            ini->endArray();
        }


        ini->endGroup();
    }

    ini->endGroup();

    qDebug() << ini->value("/mainwindow/size");
    qDebug() << ini->value("/mainwindow/fullScreen");
}

void SettingsMgr::writeTest2()
{
    QString section = "sysParam";
    writeBool(section, "key1", true, "qwqw");
    writeInteger(section, "key2", 1212, "12www");
    writeDouble(section, "key3", 3.1415, "q12wqw");
    writeString(section, "key4", "aaaaaa", "qwqqqw");

    qDebug() << readBool(section, "key1", true);
    qDebug() << readInteger(section, "key2", 0);
    qDebug() << readDouble(section, "key3", 0.1);
    qDebug() << readString(section, "key4", "");

    qDebug() << readBool(section, "key5", false);
    qDebug() << readInteger(section, "key5", 123456);
    qDebug() << readDouble(section, "key5", 0.1);
    qDebug() << readString(section, "key5", "--");


    SettingParamList list;
    list.push_back({"功率", "20"});
    list.push_back({"占空比", "50"});
    list.push_back({"时间", "23"});
    list.push_back({"电流", "1"});
    list.push_back({"电压", "23"});

    writeArray(section, "level1", list, "一档");

    SettingParamList readlist = readArray(section, "level1");
}

QString SettingsMgr::readString(QString strSection, QString strKey, QString strDefault)
{
    QString str = QString("/%1/%2/paramValue").arg(strSection).arg(strKey);
    return  ini->value(str, strDefault).toString();
}

int SettingsMgr::readInteger(QString strSection, QString strKey, int iDefault)
{
    QString str = QString("/%1/%2/paramValue").arg(strSection).arg(strKey);
    return  ini->value(str, iDefault).toInt();
}

bool SettingsMgr::readBool(QString strSection, QString strKey, bool bDefault)
{
    QString str = QString("/%1/%2/paramValue").arg(strSection).arg(strKey);
    return  ini->value(str, bDefault).toBool();
}

double SettingsMgr::readDouble(QString strSection, QString strKey, double dDefault)
{
    QString str = QString("/%1/%2/paramValue").arg(strSection).arg(strKey);
    return  ini->value(str, dDefault).toDouble();
}

SettingParamList SettingsMgr::readArray(QString strSection, QString strKey)
{
    SettingParamList paramList;
    ini->beginGroup(strSection + "_" + strKey);

    //array
    {
        int size = ini->beginReadArray(strKey);

        {
            for (int i = 0; i < size; ++i)
            {
                ini->setArrayIndex(i);
                SettingParam param;

                param.paramName = ini->value("aaDescribe", "").toString();
                param.paramValue = ini->value("paramValue", "").toString();

                paramList.append(param);
            }
        }

        ini->endArray();
    }

    ini->endGroup();

    return paramList;
}

void SettingsMgr::writeString(QString strSection, QString strKey, QString strValue, QString strDescribe)
{
    QString str;
    str = QString("/%1/%2/aaDescribe").arg(strSection).arg(strKey);
    ini->setValue(str,strDescribe);

    str = QString("/%1/%2/paramValue").arg(strSection).arg(strKey);
    ini->setValue(str,strValue);
}

void SettingsMgr::writeInteger(QString strSection, QString strKey, int iValue, QString strDescribe)
{
    QString str;
    str = QString("/%1/%2/aaDescribe").arg(strSection).arg(strKey);
    ini->setValue(str,strDescribe);

    str = QString("/%1/%2/paramValue").arg(strSection).arg(strKey);
    ini->setValue(str,iValue);
}

void SettingsMgr::writeBool(QString strSection, QString strKey, bool bValue, QString strDescribe)
{
    QString str;
    str = QString("/%1/%2/aaDescribe").arg(strSection).arg(strKey);
    ini->setValue(str,strDescribe);

    str = QString("/%1/%2/paramValue").arg(strSection).arg(strKey);
    ini->setValue(str,bValue);
}

void SettingsMgr::writeDouble(QString strSection, QString strKey, double dValue, QString strDescribe)
{
    QString str;
    str = QString("/%1/%2/aaDescribe").arg(strSection).arg(strKey);
    ini->setValue(str,strDescribe);

    str = QString("/%1/%2/paramValue").arg(strSection).arg(strKey);
    ini->setValue(str,dValue);
}

void SettingsMgr::writeArray(QString strSection, QString strKey, SettingParamList params, QString strDescribe)
{
    ini->beginGroup(strSection + "_" + strKey);

    ini->setValue("aaDescribe", strDescribe);

    //array
    {
        ini->beginWriteArray(strKey);

        {
            for (int i = 0; i < params.size(); ++i)
            {
                ini->setArrayIndex(i);

                QString str;
                str = QString("aaDescribe");
                ini->setValue(str,params[i].paramName);

                str = QString("paramValue");
                ini->setValue(str,params[i].paramValue);
            }
        }

        ini->endArray();
    }

    ini->endGroup();
}
